package com.burger.counter.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.burger.counter.beans.User;
import com.burger.counter.business.UserBusiness;

/**
 * Servlet implementation class UpdateUserData
 */
public class UpdateUserData extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateUserData() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			doPost(request, response);
		}
		catch(Exception e)
		{
			System.err.println(e);
		}		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			UserBusiness userBI = new UserBusiness();
			String firstName ="";
			String lastName ="";
			String email ="";
			Integer zip = -1;
			String dateOfBirth="";
			Integer regionData =-1;
			String password ="";
			String isAdminOrCustomer ="C";
			Boolean valid = true;
			HttpSession session = request.getSession(true);
			Integer userId = 0;
			
			if(request.getParameter("firstName") == null || 
					request.getParameter("firstName") == "")
			{
				valid = false;
			}
	
			if(request.getParameter("lastName") == null || 
					request.getParameter("lastName") == "")
			{
				valid = false;
			}
			if(request.getParameter("zip") == null || 
					request.getParameter("zip") == "")
			{
				valid = false;
			}
			
			if(request.getParameter("dateOfBirth") == null || 
					request.getParameter("dateOfBirth") == "")
			{
				valid = false;
			}
	
			if(request.getParameter("regionData") == null || 
					request.getParameter("regionData") == "")
			{
				valid = false;
			}
			if(request.getParameter("password1") == null || 
					request.getParameter("password1") == "")
			{
				valid = false;
			}
			
			if(valid)
			{
				firstName = request.getParameter("firstName").toString();
				lastName = request.getParameter("lastName").toString();
				email = request.getParameter("email").toString();
				zip = Integer.parseInt(request.getParameter("zip"));
				dateOfBirth = request.getParameter("dateOfBirth").toString();
				regionData = Integer.parseInt(request.getParameter("regionData"));
				password = request.getParameter("password1").toString();
				isAdminOrCustomer = "C";
				userId = (Integer) session.getAttribute("UserId");
				
				try 
				{
					userBI.updateUserData
							(firstName, 
								lastName, 
								email, 
								zip, 
								dateOfBirth, 
								regionData, 
								password, 
								isAdminOrCustomer,
								userId);
				} 
				catch (Exception e) 
				{
						e.printStackTrace();
				}
					
			}
			else
			{
				System.out.println("Data not valid for insert operation.");
			}
				
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/burger_type.jsp");
			dispatcher.forward(request, response);
		}
		catch(Exception e)
		{
			System.err.println(e);
		}
	}
}
